package com.hao.tool.extra.simple;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HtmlUtil;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * 从文本中取出html标签
 *
 * @author HaoZ on CMHK.hainan
 * @date 2021/6/1 上午9:08
 */
public class HtmlTextClearTool {

    public static void main(String[] args) throws IOException {
        HtmlTextClearTool htmlTextClearTool = new HtmlTextClearTool();
        htmlTextClearTool.execute();
    }

    public void execute() throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入：【需去除HTML标签的文件路径】");
        String filePath = scan.next();
        System.out.println("信息处理中，请稍等······");


        File sourceFile = new File(filePath);
        String content = FileUtil.readUtf8String(sourceFile);

        content = HtmlUtil.cleanHtmlTag(content);
        content = HtmlUtil.unescape(content);
        content = StringEscapeUtils.unescapeJava(content);
        System.out.println(content);
        String fileName = sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf("."));
        File textFile = new File(sourceFile.getParent() + File.separator
                + fileName + "_tool" + ".txt");
        FileUtil.writeString(content, textFile, Charset.defaultCharset());

        System.out.println("---------- 信息已处理，请查看 ----------");
        System.out.println(textFile.getAbsolutePath());
    }

}
